<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">
                <ul id="pagination-demo" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
    <script src="./libs/http.js"></script>
    <script src="./libs/jquery.twbsPagination.js"></script>
    <!-- 文章列表模板 -->
    <script id="art-temp" type="text/html">
        {{each data.data v}}
        <tr>
            <td>{{v.title}}</td>
            <td>{{v.author}}</td>
            <td>{{v.category}}</td>
            <td class="text-center">{{v.date}}</td>
            <td class="text-center">{{v.state}}</td>
            <td class="text-center">
                <a href="article_edit.html?id={{v.id}}" class="btn btn-default btn-xs">编辑</a>
                <a data-id="{{v.id}}" href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
            </td>
        </tr>
        {{/each}}
    </script>

    <!-- 文章分类模板 -->
    <script id="cat-temp" type="text/html">
        <option value="">所有分类</option>
        {{each data v}}
        <option value="{{v.id}}">{{v.name}}</option>
        {{/each}}
        
    </script>
    <script>

        $(function () {
            // 展示文章列表页面时让 左侧文章列表导航高亮
            $('.level02>li:eq(0)', window.parent.document).addClass('active').siblings().removeClass('active');

            // 获取所有文章
            function getArt(option = {}, initPage = true) {
                $.ajax({
                    type: 'get',
                    url: BigNew.article_query,
                    data: {
                        key: option.key,
                        type: option.type,
                        state: option.state,
                        page: option.page,
                        perpage: option.perpage || 10
                    },
                    success: function (backData) {
                        console.log(backData);
                        if (backData.code === 200) {
                            $('tbody').html(template('art-temp', backData));
                            if (initPage) {
                                // 销毁分页器
                                $('#pagination-demo').twbsPagination('destroy');
                                if (backData.data.data.length !== 0) {
                                    // 初始化分页器
                                    $('#pagination-demo').twbsPagination({
                                        totalPages: backData.data.totalPage,
                                        visiblePages: 7,
                                        startPage: 1,
                                        hideOnlyOnePage: true,
                                        first: '首页',
                                        prev: '上一页',
                                        next: '下一页',
                                        last: '尾页',
                                        onPageClick: function (event, page) {
                                            $('#page-content').text('Page ' + page);
                                            if (!initPage) { // 防止分页器初始化时,重复获取第一页数据
                                                getArt({ type: $('#selCategory').val(), state: $('#selStatus').val(), page }, false)
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                })
                initPage ? setTimeout(() => initPage = false, 500) : null;
            }
            getArt()


            // 获取所有类别
            $.ajax({
                type: 'get',
                url: BigNew.category_list,
                success: function (backData) {
                    if (backData.code === 200) {
                        $('#selCategory').html(template('cat-temp', backData));
                    }
                }
            })

            ~function () {
                let oldType = '';
                let oldState = '';
                // 过滤指定分类和状态文章
                $('#btnSearch').on('click', function (e) {
                    e.preventDefault();
                    const type = $('#selCategory').val(),
                        state = $('#selStatus').val();
                    // 防止重复点击同类筛选
                    if (type === oldType && oldState === state) return;
                    [oldType, oldState] = [type, state];
                    getArt({
                        type,
                        state
                    });
                })
            }();

            // 删除文章
            $('tbody').on('click', '.text-center .delete', function () {
                $.ajax({
                    type: 'post',
                    url: BigNew.article_delete,
                    data: { id: $(this).attr('data-id') },
                    success: (backData) => {
                        console.log(backData);
                        if (backData.code === 204) {
                            $(this).parent().parent().remove();
                        }
                    }
                })
            })

            // 发布文章
            // $('#release_btn').on('click', function () {
            //     // $('.level02>li:eq(1)', window.parent.document).addClass('active').siblings().removeClass('active');
            // })

            // $('tbody').on('click', '.text-center .btn-default', function () {
            //     // $('.level02>li:eq(1)', window.parent.document).addClass('active').siblings().removeClass('active');

            // })
        })
    </script>
</body>


</html>